package com.wd.health.view.activity.home;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wd.health.R;
import com.wd.health.app.App;
import com.wd.health.model.adapter.PopAdapter;
import com.wd.health.model.adapter.SearchLsAdapter;
import com.wd.health.model.bean.PopularBean;
import com.wd.health.model.bean.SearchBean;
import com.wd.health.utils.Person;
import com.wd.health.utils.RetrofitManager;
import com.wd.health.utils.SQLiteDao;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class SearchActivity extends AppCompatActivity {

    private ImageView img_back;
    private RecyclerView rv_search;
    private TextView tv_sousuo;
    private EditText edit_sousuo;
    private RecyclerView rv_search_ls;
    private SearchLsAdapter searchLsAdapter;
    private LinearLayout lin_search_01;
    private String s;
    private LinearLayout lin_search_02;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initView();
        initPop();
        initLs();
    }

    private void initLs() {
        final SQLiteDao dao=new SQLiteDao(getApplicationContext());
        List<Person> persons=dao.findAll();
        searchLsAdapter = new SearchLsAdapter(persons,getApplicationContext());
        rv_search_ls.setAdapter(searchLsAdapter);
        searchLsAdapter.notifyDataSetChanged();
        searchLsAdapter.setGetListener(new SearchLsAdapter.GetListener() {
            @Override
            public void onClick(int id) {
                dao.delete(id);
                searchLsAdapter.notifyDataSetChanged();
            }
        });
    }

    private void initPop() {
        RetrofitManager.getInstance().getPopData(new Observer<PopularBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(PopularBean popularBean) {
                List<PopularBean.ResultBean> list = popularBean.getResult();
                PopAdapter popAdapter = new PopAdapter(list, App.getAppContext());
                rv_search.setAdapter(popAdapter);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }


    private void initView() {
        img_back = findViewById(R.id.img_back);
        rv_search = findViewById(R.id.rv_search);
        tv_sousuo = findViewById(R.id.tv_sousuo);
        rv_search_ls = findViewById(R.id.rv_search_ls);

        lin_search_01 = findViewById(R.id.lin_search_01);
        lin_search_02 = findViewById(R.id.lin_search_02);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
        linearLayoutManager.setStackFromEnd(true);
        linearLayoutManager.setReverseLayout(true);
        rv_search_ls.setLayoutManager(linearLayoutManager);
        edit_sousuo = findViewById(R.id.edit_sousuo);

        FlexboxLayoutManager manager = new FlexboxLayoutManager();
        //设置主轴排列方式
        manager.setFlexDirection(FlexDirection.ROW);
        //设置是否换行
        manager.setFlexWrap(FlexWrap.WRAP);
        manager.setAlignItems(AlignItems.STRETCH);

        rv_search.setLayoutManager(manager);

        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tv_sousuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                s = edit_sousuo.getText().toString();
                SQLiteDao dao=new SQLiteDao(getApplicationContext());
                dao.add(s,"1");
                initLs();
                initSouSuo();
            }
        });
    }

    private void initSouSuo() {
        RetrofitManager.getInstance().getSearchData(s, new Observer<SearchBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(SearchBean searchBean) {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}